package com.dingpeikeji.quickdeer.modules.applycargo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.applycargo.entity.TApplycargo;
import com.dingpeikeji.quickdeer.modules.applycargo.service.TApplycargoService;

/**
 * 客运公司车辆加盟Controller
 * @author dp
 * @version 2017-01-04
 */
@Controller
@RequestMapping(value = "${adminPath}/applycargo/tApplycargo")
public class TApplycargoController extends BaseController {

	@Autowired
	private TApplycargoService tApplycargoService;
	
	@ModelAttribute
	public TApplycargo get(@RequestParam(required=false) String acid) {
		TApplycargo entity = null;
		if (StringUtils.isNotBlank(acid)){
			entity = tApplycargoService.get(acid);
		}
		if (entity == null){
			entity = new TApplycargo();
		}
		return entity;
	}
	
	@RequiresPermissions("applycargo:tApplycargo:view")
	@RequestMapping(value = {"list", ""})
	public String list(TApplycargo tApplycargo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TApplycargo> page = tApplycargoService.findPage(new Page<TApplycargo>(request, response), tApplycargo); 
		model.addAttribute("page", page);
		model.addAttribute("tApplycargo", tApplycargo);
		return "modules/applycargo/tApplycargoList";
	}

	@RequiresPermissions("applycargo:tApplycargo:view")
	@RequestMapping(value = "form")
	public String form(TApplycargo tApplycargo, Model model) {
		model.addAttribute("tApplycargo", tApplycargo);
		return "modules/applycargo/tApplycargoForm";
	}

	@RequiresPermissions("applycargo:tApplycargo:edit")
	@RequestMapping(value = "save")
	public String save(TApplycargo tApplycargo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tApplycargo)){
			return form(tApplycargo, model);
		}
		if (tApplycargo.getAcid() != null) {
			tApplycargo.setAuditstatus(TApplycargo.AUDITSTATUS_DONE);
			tApplycargo.setAuditor(UserUtils.getUser());
		}
		
		tApplycargoService.save(tApplycargo);
		addMessage(redirectAttributes, "保存客运公司车辆加盟成功");
		return "redirect:"+Global.getAdminPath()+"/applycargo/tApplycargo/?repage";
	}
	
	@RequiresPermissions("applycargo:tApplycargo:edit")
	@RequestMapping(value = "delete")
	public String delete(TApplycargo tApplycargo, RedirectAttributes redirectAttributes) {
		tApplycargoService.delete(tApplycargo);
		addMessage(redirectAttributes, "删除客运公司车辆加盟成功");
		return "redirect:"+Global.getAdminPath()+"/applycargo/tApplycargo/?repage";
	}

}